package com.ydlclass.compress.Factory;

import com.ydlclass.compress.CompressWrapper;
import com.ydlclass.compress.Impl.GzipCompress;
import com.ydlclass.serialize.Impl.HessianSerializer;
import com.ydlclass.serialize.Impl.JdkSerializer;
import com.ydlclass.serialize.Impl.JsonSerializer;
import com.ydlclass.serialize.SerializeWrapper;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 序列化工厂方法
 * @Author duWenJian
 * @Date 2023/11/3 14:28
 * @Version 1.0
 */
public class CompressFactory {
    private final static ConcurrentHashMap<String, CompressWrapper> COMPRESSOR_CACHE = new ConcurrentHashMap<>(8);
    private final static ConcurrentHashMap<Byte, CompressWrapper> COMPRESSOR_CACHE_CODE = new ConcurrentHashMap<>(8);
    static {
        CompressWrapper gzip = new CompressWrapper((byte) 1,"gzip",new GzipCompress());

        COMPRESSOR_CACHE.put("gzip", gzip);

        COMPRESSOR_CACHE_CODE.put(gzip.getCode(), gzip);

    }

    /**
     * 使用工厂方法，获取一个CompressWrapper
     * @param compressType 压缩的类型
     * @return
     */
    public static CompressWrapper getCompressor(String compressType) {
        CompressWrapper compressWrapper = COMPRESSOR_CACHE.get(compressType);
        if (compressWrapper == null){
            return COMPRESSOR_CACHE.get("gzip");
        }
        return COMPRESSOR_CACHE.get(compressType);
    }
    public static CompressWrapper getCompressor(byte compressType) {
        CompressWrapper compressWrapper = COMPRESSOR_CACHE_CODE.get(compressType);
        if (compressWrapper == null){
            return COMPRESSOR_CACHE_CODE.get((byte) 1);
        }
        return COMPRESSOR_CACHE_CODE.get(compressType);
    }

}
